﻿/// <summary>
/// Copyright(c) 2008-2009北京玖富时代投资顾问有限公司, All Rights Reserved.
/// 
/// 通用公共方法--文件操作类,包括文件读写，文件夹操作等
/// 
/// 版本：Ver 1.0
/// 作者：【周燕龙】
/// 时间：2009-09-08
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Hisuntech.SystemFramework.Common
{
    public class FileOperate
    {      

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="fileName">文件的绝对路径</param>
        /// <returns></returns>
        public static byte[] ReadFile(string fileName)
        {
            FileStream pFileStream = null;
            byte[] pReadByte = new byte[0];

            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filename">文件的绝对路径</param>
        public static void Delete(string filename)
        {
            File.Delete(filename);
        }


        


        /// <summary>
        /// 确定指定的文件是否存在
        /// </summary>
        /// <param name="filename">文件的绝对路径</param>
        /// <returns>文件已存在返回真，否则为假</returns>
        public static bool Exists(string filename)
        {
            return File.Exists(filename);
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns>目录信息</returns>
        public static DirectoryInfo CreateDirectory(string path)
        {
            if (Directory.Exists(path) == false)
            {
                return Directory.CreateDirectory(path);
            }
            return new DirectoryInfo(path);
        }




        public static void ConvertHtmlUrlToMht(string htmlUrl, string path, string filename)
        {
            string _CharSet = "gb2312";
            //htmlUrl = "file://" + htmlUrl;
            int i = htmlUrl.IndexOf("=");
            string id = htmlUrl.Substring(i + 1, htmlUrl.Length - i - 1);

            string imagecont = "------=_NextPart_000_0003_01C8F883.0FF50820 " +
                      "Content-Type: image/jpeg " +
                      "Content-Transfer-Encoding: base64 " +
                      "/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAUAAA/+4ADkFkb2JlAGTAAAAAAf/b " +
                      "AIQAAgICAgICAgICAgMCAgIDBAMCAgMEBQQEBAQEBQYFBQUFBQUGBgcHCAcHBgkJCgoJCQwMDAwM " +
                      "DAwMDAwMDAwMDAEDAwMFBAUJBgYJDQsJCw0PDg4ODg8PDAwMDAwPDwwMDAwMDA8MDAwMDAwMDAwM " +
                      "DAwMDAwMDAwMDAwMDAwMDAwM/8AAEQgDLAAEAwERAAIRAQMRAf/EAFsAAAMBAQEAAAAAAAAAAAAA " +
                      "AAABAgMECQEBAQEBAQAAAAAAAAAAAAAAAAECBQcQAQEBAQEBAQAAAAAAAAAAAAARARJRYQIRAQEB " +
                      "AAAAAAAAAAAAAAAAAAARAf/aAAwDAQACEQMRAD8A8eHCeqgHXM8Vg1QAcRYY0AAKRoxQBxFhinNF " +
                      "ikaAKRoCnBYaKqDUNFUjRwajTj6Fb854BgqYjUMVSNKRs4LFI0qYNQ5qLFDTo53wRoinBqKiNQxV " +
                      "o2JosUjS0aa8/nwRcGoqI1DFUjZzRYpGlTEahzfBWoqufqEWKpGjg1FIogsUjSpg1Aiq50I0FEFi " +
                      "kaAKRoAqDUNFANON+C3GggA4NQ0UAAVMGoEAAAAsbAAAAAAcRYY0AAHN80GwyAAAH//Z " +
                       "------=_NextPart_000_0003_01C8F883.0FF50820--";


            string ylujv = path + filename;
            filename = filename.Substring(0, filename.Length - 4);
            string NameFiles = filename + ".mht";
            string filePath = path + NameFiles;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            CDO.Message message = new CDO.MessageClass();
            CDO.Configuration c = new CDO.ConfigurationClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.Configuration = c;
            message.CreateMHTMLBody(htmlUrl, CDO.CdoMHTMLFlags.cdoSuppressObjects, "", "");
            stream = message.GetStream();
            stream.Charset = _CharSet;//字符集
            stream.SaveToFile(filePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);//保存MHT
            stream.Close();


            StreamReader srs = new StreamReader(filePath);
            string filecont = srs.ReadToEnd();
            byte[] cont = Encoding.GetEncoding(_CharSet).GetBytes(filecont);
            string contents = Encoding.Default.GetString(cont);
            contents += imagecont;
            string title = "</title>";
            int pos = contents.IndexOf(title);
            pos = pos + title.Length;
            contents = contents.Insert(pos, @"<META http-equiv='Content-Type' content='text/html'; charset='utf-8'>");
            contents = contents.Replace("?<!DOCTYPE", "<!DOCTYPE");
            srs.Close();
            srs.Dispose();
            string name = filePath;
            StreamWriter fs = new StreamWriter(name);
            fs.Write(contents);
            fs.Close();
            fs.Dispose();
        }

        public static void ConvertHtmlUrlToMht(string htmlUrl, string path, string filename,int num)
        {
            string _CharSet = "gb2312";
            htmlUrl = "file://" + htmlUrl;
            int i = htmlUrl.IndexOf("=");
            string id = htmlUrl.Substring(i + 1, htmlUrl.Length - i - 1);

            string imagecont = "------=_NextPart_000_0003_01C8F883.0FF50820 " +
                      "Content-Type: image/jpeg " +
                      "Content-Transfer-Encoding: base64 " +
                      "/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAUAAA/+4ADkFkb2JlAGTAAAAAAf/b " +
                      "AIQAAgICAgICAgICAgMCAgIDBAMCAgMEBQQEBAQEBQYFBQUFBQUGBgcHCAcHBgkJCgoJCQwMDAwM " +
                      "DAwMDAwMDAwMDAEDAwMFBAUJBgYJDQsJCw0PDg4ODg8PDAwMDAwPDwwMDAwMDA8MDAwMDAwMDAwM " +
                      "DAwMDAwMDAwMDAwMDAwMDAwM/8AAEQgDLAAEAwERAAIRAQMRAf/EAFsAAAMBAQEAAAAAAAAAAAAA " +
                      "AAABAgMECQEBAQEBAQAAAAAAAAAAAAAAAAECBQcQAQEBAQEBAQAAAAAAAAAAAAARARJRYQIRAQEB " +
                      "AAAAAAAAAAAAAAAAAAARAf/aAAwDAQACEQMRAD8A8eHCeqgHXM8Vg1QAcRYY0AAKRoxQBxFhinNF " +
                      "ikaAKRoCnBYaKqDUNFUjRwajTj6Fb854BgqYjUMVSNKRs4LFI0qYNQ5qLFDTo53wRoinBqKiNQxV " +
                      "o2JosUjS0aa8/nwRcGoqI1DFUjZzRYpGlTEahzfBWoqufqEWKpGjg1FIogsUjSpg1Aiq50I0FEFi " +
                      "kaAKRoAqDUNFANON+C3GggA4NQ0UAAVMGoEAAAAsbAAAAAAcRYY0AAHN80GwyAAAH//Z " +
                       "------=_NextPart_000_0003_01C8F883.0FF50820--";


            string ylujv = path + filename;
            filename = filename.Substring(0, filename.Length - 4);
            string NameFiles = filename + ".mht";
            string filePath = path + NameFiles;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            CDO.Message message = new CDO.MessageClass();
            CDO.Configuration c = new CDO.ConfigurationClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.Configuration = c;
            message.CreateMHTMLBody(htmlUrl, CDO.CdoMHTMLFlags.cdoSuppressObjects, "", "");
            stream = message.GetStream();
            stream.Charset = _CharSet;//字符集
            stream.SaveToFile(filePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);//保存MHT
            stream.Close();


            //StreamReader srs = new StreamReader(filePath);
            //string filecont = srs.ReadToEnd();
            //byte[] cont = Encoding.GetEncoding(_CharSet).GetBytes(filecont);
            //string contents = Encoding.Default.GetString(cont);
            //contents += imagecont;
            //string title = "</title>";
            //int pos = contents.IndexOf(title);
            //pos = pos + title.Length;
            //contents = contents.Insert(pos, @"<META http-equiv='Content-Type' content='text/html'; charset='utf-8'>");
            //contents = contents.Replace("?<!DOCTYPE", "<!DOCTYPE");
            //srs.Close();
            //srs.Dispose();
            //string name = filePath;
            //StreamWriter fs = new StreamWriter(name);
            //fs.Write(contents);
            //fs.Close();
            //fs.Dispose();
        }
       
    }
   
}
